<!--* 下拉树形选择 组件-->
<template>
<!--  <el-select ref="select" style="min-width: 260px" :value="value" v-model="valueName" collapse-tags :multiple="multiple" :clearable="clearable" @change="changeValue">-->
<!--    -->
  <el-select
      ref="select"
      size="small"
      :placeholder="placeholder"
      :disabled="disabled"
      :value="value"
      v-model="valueName"
      collapse-tags
      :multiple="multiple"
      :clearable="clearable"
      @clear="clearHandle"
      @change="changeValue">
    <el-option :value="valueName" class="options">

      <treeModule
        v-if="showTree"
        :nodeclickAllow="false"
        node-key="id"
        ref="tree"
        :defaultCheckedKeys="defaultCheckedKeys"
        :defaultExpandAll="true"
        :defaultProps="props"
        :treeData="options"
        :checkStrictly="true"
        :showSheckbox="true"
        @changeList="changeList"
        @nodeclick="nodeclick"
        @handleClick="handleClick">
      </treeModule>

    </el-option>
  </el-select>
</template>
<script>
import treeModule from '@/components/treeModule.vue'
export default {
  name: 'el-tree-select',
  components:{
    treeModule,
  },
  props: {
    // 配置项
    props: {
      type: Object,
      default: () => {
        return {
          value: 'id',
          children: 'child',
          label: 'name'
        }
      }
    },
    // 初始值（单选）
    value: {
      type: Object,
      default: () => {
        return {}
      }
    },
    // 初始值（多选）
    valueMultiple: {
      type: Array,
      default: () => {
        return []
      }
    },
    // 可清空选项
    clearable: {
      type: Boolean,
      default: false
    },
    // 自动收起
    accordion: {
      type: Boolean,
      default: false
    },
    // 是否支持多选
    multiple: {
      type: Boolean,
      default: true
    },
    // 默认选中的节点
    defaultCheckedKeys:  {
      type: Array,
      default () {
        return []
      }
    },
    placeholder: {
      type: String,
      default: '请选择'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    language: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      options: [],
      resultValue: [], // 传给父组件的数组对象值
      valueName: [], // 输入框显示值
      showTree: false,
    }
  },
  watch: {
    value () {
      this.resultValue = this.valueMultiple // 初始值
      this.initHandle()
    }
  },
  mounted () {
    if(!this.disabled){
      this.getSelectTreeList()
    }
    // this.getSelectTreeList()
    this.resultValue = this.valueMultiple // 初始值
    this.initHandle()
  },
  methods: {
    // 获取 型号 tree
    getSelectTreeList: function () {
      const vm = this;
      const param = {
        "type": 2,
        "language": vm.language
      }
      vm.$store.dispatch('moduleList', param,"POST")
        .then((res) => {
          const { data:{list=[]},code } = res
          if (code == 0) {
            this.options=list;
            // console.log('this.options', this.options)
            this.showTree = true
          }
      })
    },
    // 初始化显示
    initHandle () {
      if (this.resultValue) {
        // this.resultValue.forEach((item) => {
        //   this.valueName.push(item.name)
        // })
        this.valueName=this.resultValue.map((item)=>{
          return item.name
        }).join(',')
        // console.log('this.valueName', this.valueName)
      }
      this.initScroll()
    },
    // 初始化滚动条
    initScroll () {
      this.$nextTick(() => {
        let scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
        let scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
        scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;'
        scrollBar.forEach((ele) => (ele.style.width = 0))
      })
    },
    // 从输入框中直接删除某个值时
    changeValue (val) {
      // 多选（同时删掉传给父组件中多余的值，再传给父组件）
      if(this.resultValue&&this.resultValue.length>0){
        this.resultValue.map((item, index) => {
          let i = val.indexOf(item.name)
          if (i === -1) {
            this.resultValue.splice(index, 1)
          }
        })
        this.$emit('getValue', this.resultValue)
      }
    },
    // 勾选 /反选
    handleCheckChange (data, checked, indeterminate) {
      console.log('data', data)
      console.log('checked', checked)
      console.log('indeterminate', indeterminate)
      this.valueName = [].join(',')
      if (checked) {
        // 选中
        // if (!data.child.length) {
        //   this.resultValue.push(data)
        // }
        if (!data.child.length) {
          this.resultValue.push(data)
        }
      } else {
        // 取消勾选
        const { name } = data
        this.resultValue.map((item, index) => {
          if (name === item.name) {
            this.resultValue.splice(index, 1)
          }
        })
      }
      this.resultValue.forEach((item) => {
        this.valueName.push(item.name) // 输入框显示值
      })
      // console.log('this.resultValue', this.resultValue)
      this.$emit('getValue', this.resultValue)
    },
    // 默认展开全部
    openTreeList (list) {
      list.forEach((item) => {
        this.openTree.push(item.id)
        if (item.child && item.child.length) {
          this.openTreeList(item.child)
        }
      })
    },

    // 清除选中
    clearHandle () {
      // this.valueName = []
      // this.initHandle()
      this.resultValue = []
      this.clearSelected()
      this.$refs.tree.clearHandle();
      this.$emit('getValue', this.resultValue)
    },
    // // 清空选中样式
    clearSelected () {
      let allNode = document.querySelectorAll('#tree-option .el-tree-node')
      allNode.forEach((element) => element.classList.remove('is-current'))
    },

    changeList(arr){
      console.log('arr', arr)
      // this.treeData = arr
    },
    // 点击节点
    nodeclick(obj, node, self) {
      this.form = obj
    },
    // 复选变单选
    handleClick(data, checked) {
      if(checked){
        this.valueName = data.name;
        this.$emit('getValue', data)
      }else {
        this.valueName = [].join(',')
      }
      this.sleep(50).then(()=>{
        this.$refs.select.blur();
      })
    },

  }
}
</script>
<style lang="less" scoped>
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
  height: auto;
  max-height: 300px;
  padding: 0;
  overflow: hidden;
  overflow-y: auto;
}

.el-select-dropdown__item.selected {
  font-weight: normal;
}

.el-tree .el-tree-node__content {
  height: auto;
  padding: 0 20px;
}

.el-tree-node__label {
  font-weight: normal;
}

.el-tree .is-current .el-tree-node__label {
  color: #409eff;
  font-weight: 700;
}

.el-tree .is-current .el-tree-node__children .el-tree-node__label {
  color: #606266;
  font-weight: normal;
}

.el-popper {
  z-index: 9999;
}

.ification_col {
  width: 20px;
  height: 10px;
  display: inline-block;
}

.el-select-dropdown__item::-webkit-scrollbar {
  display: none !important;
}

.el-select {
  ::v-deep.el-tag__close {
    display: none !important; //隐藏在下拉框多选时单个删除的按钮
  }
}
</style>

